<!DOCTYPE html>
<html>
    <head>
        <title>Pick-up Game</title>
        <link rel="stylesheet" href="style.css" type="text/css" />
        <script src="http://code.jquery.com/jquery-latest.js"></script>
	<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
	<script>
	    $(document).ready(function(){
		$("#infoForm").validate({
			rules: {
				  confirm: { equalTo: "#pass" }
			}
		});
		$("#privacy").validate();
		$("#details_form").validate();
		$("#scheduleform").validate();
		$("#scheduledelete").validate();
		$("#sportsForm").validate();
	    });
	</script>
    </head>

    <body>

        <?php include_once('menu.php') ?>
        
	 <div id="content" class="container">
        <?php if (!empty($_SESSION['username'])) : ?>
            
		<h2>Personal Information</h2>
		<hr>

            <form action="saveSettings.php" method="post" id="infoForm">
                    <table>
                        <tr><td>First Name</td><td><?php echo $_SESSION['firstname'] ?></td></tr>
                        <tr><td>Last Name</td><td><?php echo $_SESSION['lastname'] ?></td></tr>
                        <tr><td>E-mail</td><td><input type="text" name="email" value="<?php echo $_SESSION['email'] ?>" class="email"></td></tr>
                        <tr><td>Password</td><td><input type="password" name="pass" id="pass"></td></tr>
                        <tr><td>Confirm Password</td><td><input type="password" name="confirm" id="confirm"></td></tr>
                     </table>
                     
                     <hr>
                     <table>
                        <tr><td>Birthday</td><td><?php echo $_SESSION['birthday'] ?></td></tr>
                        <tr><td>Address</td><td><input type="text" name="address" value="<?php echo $_SESSION['address'] ?>" class="required"></td></tr>
                        <tr><td>City</td><td><input type="text" name="city" value="<?php echo $_SESSION['city'] ?>" class="required"></td></tr>
                        <?php // create countries dropdown

			    // connect to database
			    $conn_str = 'host=dbsrv1.cdf.toronto.edu dbname=csc309g23 user=csc309g23 password=ipheid5h';
			    $conn = pg_connect($conn_str);
        
			    $query = 'SELECT * FROM countries';
			    $result = pg_query($conn, $query);
			    ?>
			    <tr><td>Country</td>
                                <td><select name="country">
					    <option value=''>-</option>
                                        <?php while ($row = pg_fetch_row($result)) { ?>
                                            <option <?php if ($row[0] == $_SESSION['country']) {echo 'selected=/"selected/"';}?> ><?= $row[0] ?></option>
                                        <?php } ?>
                                    </select>
                            </td></tr>
                        
                        
                        <tr><td></td><td><input type="submit" value="Save"></td></tr>
                     </table>
                     <input type="hidden" name="settingType" value="personal">
            </form>
                     
                <h2>Display Picture</h2>
                <hr>
		 <center><img src="<?php echo $_SESSION['display'] ?>" class="border"/></center>
                <hr>
		<form action="saveSettings.php" method="post" name="details_form" id="details_form" enctype="multipart/form-data">
		<table>
		  <tr>
		    <td><label for="display_pic">Upload a displlay picture</label></td>
		    <td><input type="file" name="display_pic" id="display_pic" />
                  <tr><td></td><td><input id="send" name="send" type="submit" value="Submit"/></td></tr>                   
                </table>
                <input type="hidden" name="settingType" value="display">
            </form>
        
                
                <h2>Privacy Settings</h2>
                <hr>
                <form action="saveSettings.php" method="post" id="privacy">
                    <table>
                        <tr><td>Show first name</td><td>
                                <select name="showfirstname">
                                    <option value="t">Yes</option>
				    <option value="f" <?php echo ($_SESSION['showfirstname'] == 'f') ? 'selected="selected"' : ''; ?>>No</option>
                                </select>
                            </td></tr>
                        <tr><td>Show last name</td><td>
                                <select name="showlastname">
                                    <option value="t">Yes</option>
                                    <option value="f" <?php echo ($_SESSION['showlastname'] == 'f') ? 'selected="selected"' : ''; ?>>No</option>
                                </select>
                            </td></tr>
                        <tr><td>Show birthday</td><td>
                                <select name="showbirthday">
                                    <option value="t">Yes</option>
                                    <option value="f" <?php echo ($_SESSION['showbirthday'] == 'f') ? 'selected="selected"' : ''; ?>>No</option>
                                </select>
                            </td></tr>
                        <tr><td>Show email</td><td>
                                <select name="showemail">
                                    <option value="t">Yes</option>
                                    <option value="f" <?php echo ($_SESSION['showemail'] == 'f') ? 'selected="selected"' : ''; ?>>No</option>
                                </select>
                            </td></tr>
                        <tr><td>Show address</td><td>
                                <select name="showaddress">
                                    <option value="t">Yes</option>
                                    <option value="f" <?php echo ($_SESSION['showaddress'] == 'f') ? 'selected="selected"' : ''; ?>>No</option>
                                </select>
                            </td></tr>
                            <tr><td></td><td><input type="submit" value="Save" /></td></tr>
                    </table>
            </form>
            
                <h2>Game Statistcs</h2>
                <hr>
                    <table class="verticalTable">
                        <tr><td>Games Played</td><td><?php echo $_SESSION['playgames'] ?></td></tr>
                        <tr><td>Player Rating</td><td><?php echo $_SESSION['playrating'] ?></td></tr>
                        <tr><td>Games Organized</td><td><?php echo $_SESSION['orggames'] ?></td></tr>
                        <tr><td>Organizer Rating</td><td><?php echo $_SESSION['orgrating'] ?></td></tr>
                    </table>
            
            <h2>Availability</h2>
            <hr>
        <?php
        // connect to database
        $conn_str = 'host=dbsrv1.cdf.toronto.edu dbname=csc309g23 user=csc309g23 password=ipheid5h';
        $conn = pg_connect($conn_str);
        
        // search database for member schedule
        $query = 'SELECT * FROM schedule WHERE username =\''.$_SESSION['username'].'\'';
        $result = pg_query($conn, $query);
        $times = pg_query($conn,$query);
                
        ?>
        <form action="saveSettings.php" method="post" name="scheduleform" id="scheduleform">
                <table>
                    <tr>
                        <td><label for="day">Day</label></td>
                        <td><select id="day" name="day" class="required" title="Day. This is a required field">
                            <option value="monday">Mon</option>
                            <option value="tuesday">Tue</option>
                            <option value="wednesday">Wed</option>
                            <option value="thursday">Thu</option>
                            <option value="friday">Fri</option>
                            <option value="saturday">Sat</option>
                            <option value="sunday">Sun</option>
                        </select></td>
                    </tr>
                    <tr>
                        <td><label for="stime">Start</label></td>
                        <td><input type ="text" id="stime" name="stime" class="required" title="Start time" maxlength="8"/></td>
                    </tr>
                    <tr>
                        <td><label for="etime">End</label></td>
                        <td><input type ="text" id="etime" name="etime" class="required" title="End time" maxlength="8" /></td>
                    </tr>
                    <tr><td></td>
                        <td>
			   <input type="hidden" name="settingType" value="update">
                            <input type="submit" id="submit" value="Add">
                        </td>
                        <td></td>
                    </tr>
                </table>
        </form>
        <hr>
        <form action="saveSettings.php" method="post" name="scheduledelete" id="scheduledelete">
		    <table>
			<tr><td><label for="select_timeslot">Select a Time slot to remove</label></td>
			    <td><select name="select_timeslot">
				<?php while ($row = pg_fetch_row($times)) {  ?>
				<option><?= $row[3] ?>:<?= $row[1] ?>-<?= $row[2] ?></option>
				<?php } ?>
				</select>
			    </td>
			</tr>
			<tr><td></td>
			    <td><input type="hidden" name="settingType" value="remove">
			    <input type="submit" value="Remove"/></td>
			</tr>
		    </table>
        </form>
        <hr>
        <table  class="verticalTable">
            <tr><td>Current Availability</td><td></td><td></td></tr>
            <tr><td>Day</td><td>Start Time</td><td>End Time</td></tr>
            <?php while ($row = pg_fetch_row($result)) { ?>
            <tr><td><?= $row[3] ?></td><td><?= $row[1] ?></td><td><?= $row[2]?></td></tr>
            <?php } ?>
        </table>
            
            <h2>Sports</h2>
            <hr>
            
        <?php

        // query database for all sports
        $query = 'SELECT * FROM sports';
        $result = pg_query($conn, $query);

        function check_sport($sport, $username, $conn) {
            
            $query = 'SELECT * FROM availability WHERE username = \'' . $username . '\' AND sport = \'' . $sport . '\'';
            $result = pg_query($conn, $query);
            $row = pg_fetch_row($result);
            if (pg_num_rows($result) == 0) {
                echo "rows returned" . pg_num_rows($result);
                return False;
            }
            return True;
        }
        ?>

            <form action="saveSettings.php" method="post" id="sportsForm">
                    <table>
                        <?php while ($row = pg_fetch_row($result)) { ?>
                            <tr><td><input type="checkbox" name ="<?= $row[0] ?>" value="<?= $row[0] ?>" <?php
                    if (check_sport($row[0], $_SESSION['username'], $conn)) {
                        echo "checked";
                    }
                    ?>/></td>
                                <td><label for="<?= $row[0] ?>"><?= $row[0] ?></label></td></tr>
    <?php } ?>
                        <tr><td></td><td><input type='submit' value='save'/></td><td></td></tr>
                    </table>
		    <input type="hidden" name="settingType" value="sports">
            </form>
        <?php
        else :
            header('Location: /');
        endif;
        ?>
    </div>
    </body>

</html>
